package com.bilibili.bilibili.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.android.volley.VolleyError;
import com.bilibili.bilibili.R;
import com.bilibili.bilibili.adapter.Bangindexadapter;
import com.bilibili.bilibili.bean.Bangindexbean;
import com.bilibili.bilibili.network.HttpGetData;
import com.bilibili.bilibili.network.VolleyInterface;
import com.bilibili.bilibili.utils.GsonUtil;
import com.bilibili.bilibili.utils.URLUtils;
import com.google.gson.reflect.TypeToken;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 番剧索引界面
 */

public class Bangindex extends AppCompatActivity {

    private RecyclerView recyclerView;
    private Bangindexadapter bangindexadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_bangumi_index_head);
        initid();
        initdata();
    }

    private void initdata() {
        HttpGetData.RequestGet(this, URLUtils.DRAMA_HEAD + URLUtils.BANGINDEX, "zxw", new VolleyInterface(this, VolleyInterface.mListener, VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                try {
                    String json = new String(result.getBytes("ISO-8859-1"), "UTF-8");
                    Bangindexbean bangindex = GsonUtil.parseJsonToObj(json, new TypeToken<Bangindexbean>() {
                    });
                    List<Bangindexbean.ResultEntity.CategoryEntity> category = bangindex.getResult().getCategory();
                    bangindexadapter.initdata(category);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onMyError(VolleyError error) {

            }
        });
    }

    private void initid() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
        toolbar.setTitle("详情");

        recyclerView = (RecyclerView) findViewById(R.id.bangindex_recycle);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        bangindexadapter = new Bangindexadapter(this);
        recyclerView.setAdapter(bangindexadapter);

    }

}
